'use strict';

var _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault');
Object.defineProperty(exports, '__esModule', {
  value: true,
});
exports.default = useValue;
var _objectSpread3 = _interopRequireDefault(require('@babel/runtime/helpers/objectSpread2'));
var _toConsumableArray2 = _interopRequireDefault(
  require('@babel/runtime/helpers/toConsumableArray'),
);
var _defineProperty2 = _interopRequireDefault(require('@babel/runtime/helpers/defineProperty'));
var _react = require('react');
var _antd = require('antd');
var _set = _interopRequireDefault(require('lodash/set'));
var _get = _interopRequireDefault(require('lodash/get'));
function useValue(form, _ref) {
  var name = _ref.name,
    initialValue = _ref.initialValue,
    value = _ref.value,
    rowKey = _ref.rowKey,
    expandRow = _ref.expandRow,
    min = _ref.min,
    max = _ref.max,
    rowExpandMin = _ref.rowExpandMin,
    rowExpandMax = _ref.rowExpandMax;
  var vidRef = (0, _react.useRef)(1);
  (0, _react.useEffect)(
    function () {
      if (name && initialValue !== undefined && value === undefined) {
        form.setFieldsValue((0, _defineProperty2.default)({}, name, initialValue));
      }
    },
    [form, name, initialValue, value],
  );
  /** 新建时，使用虚拟 id */
  var getVirtualItem = (0, _react.useCallback)(
    function () {
      vidRef.current = vidRef.current + 1;
      return {
        _vid: vidRef.current,
      };
    },
    [vidRef],
  );
  /** 新增一行 */
  var addItem = (0, _react.useCallback)(
    function (row) {
      var value = form.getFieldValue(name) || [];
      var newValue = (0, _toConsumableArray2.default)(value);
      var item = getVirtualItem();
      // 首层
      if (!row) {
        if (max && (newValue === null || newValue === void 0 ? void 0 : newValue.length) >= max) {
          _antd.message.error(
            '\u914D\u7F6E\u7684\u6761\u76EE\u6570\u987B\u5C0F\u4E8E\u7B49\u4E8E'.concat(
              max,
              '\u6761',
            ),
          );
          return;
        }
        newValue.push(item);
        // 深层
      } else {
        var children = row.children
          ? [].concat((0, _toConsumableArray2.default)(row.children), [item])
          : [item];
        if (
          rowExpandMax &&
          (children === null || children === void 0 ? void 0 : children.length) >= rowExpandMax
        ) {
          _antd.message.error(
            '\u914D\u7F6E\u7684\u5B50\u7EA7\u6761\u76EE\u6570\u987B\u5C0F\u4E8E\u7B49\u4E8E'.concat(
              rowExpandMax,
              '\u6761',
            ),
          );
          return;
        }
        (0, _set.default)(newValue, row.childFieldNamePrefix, children);
        expandRow(true, row);
      }
      form.setFieldsValue((0, _defineProperty2.default)({}, name, newValue));
    },
    [name, expandRow, form],
  );
  /** 删除一行 */
  var removeItem = (0, _react.useCallback)(
    function (row) {
      var value = form.getFieldValue(name) || [];
      var newValue = (0, _toConsumableArray2.default)(value);
      if (!row._parent) {
        var _newValue;
        if (
          min &&
          ((_newValue = newValue) === null || _newValue === void 0 ? void 0 : _newValue.length) <=
            min
        ) {
          _antd.message.error(
            '\u914D\u7F6E\u7684\u6761\u76EE\u6570\u987B\u5927\u4E8E\u7B49\u4E8E'.concat(
              min,
              '\u6761',
            ),
          );
          return;
        }
        newValue = newValue.filter(function (item) {
          return rowKey(item) !== rowKey(row);
        });
      } else {
        var _parent$children;
        var parent = row._parent;
        var children =
          (_parent$children = parent.children) === null || _parent$children === void 0
            ? void 0
            : _parent$children.filter(function (item) {
                return rowKey(item) !== rowKey(row);
              });
        if (
          rowExpandMin &&
          (children !== null && children !== void 0 ? children : []).length <= rowExpandMin
        ) {
          _antd.message.error(
            '\u914D\u7F6E\u7684\u5B50\u7EA7\u6761\u76EE\u6570\u987B\u5927\u4E8E\u7B49\u4E8E'.concat(
              rowExpandMin,
              '\u6761',
            ),
          );
          return;
        }
        (0, _set.default)(
          newValue,
          parent.childFieldNamePrefix,
          !!(children === null || children === void 0 ? void 0 : children.length)
            ? children
            : undefined,
        );
        if (!(children === null || children === void 0 ? void 0 : children.length))
          expandRow(false, parent);
      }
      form.setFieldsValue((0, _defineProperty2.default)({}, name, newValue));
    },
    [name, expandRow, form, rowKey],
  );
  /** 移动 */
  var moveItem = (0, _react.useCallback)(
    function (row, dragIndex, hoverIndex) {
      var _row$fieldNamePrefix$, _row$fieldNamePrefix;
      var value = form.getFieldValue(name) || [];
      var newValue = (0, _toConsumableArray2.default)(value);
      var dragChildrenName =
        (_row$fieldNamePrefix$ =
          (_row$fieldNamePrefix = row.fieldNamePrefix) === null || _row$fieldNamePrefix === void 0
            ? void 0
            : _row$fieldNamePrefix.slice(0, -1)) !== null && _row$fieldNamePrefix$ !== void 0
          ? _row$fieldNamePrefix$
          : [];
      var dragRowValue = (0, _get.default)(
        newValue,
        [].concat((0, _toConsumableArray2.default)(dragChildrenName), [dragIndex]),
      );
      if (!row._parent) {
        newValue.splice(dragIndex, 1);
        newValue.splice(hoverIndex, 0, dragRowValue);
      } else {
        var children = (0, _get.default)(newValue, dragChildrenName);
        children.splice(dragIndex, 1);
        children.splice(hoverIndex, 0, dragRowValue);
        (0, _set.default)(newValue, dragChildrenName, children);
      }
      form.setFieldsValue((0, _defineProperty2.default)({}, name, newValue));
    },
    [name, form],
  );
  /** 批量递归设置子节点的某个字段 */
  var setParentValue = (0, _react.useCallback)(
    function (row, fieldName, fieldValue) {
      var _row$fieldNamePrefix2;
      var value = form.getFieldValue(name) || [];
      var newValue = (0, _toConsumableArray2.default)(value);
      var parentNamePrefix =
        (_row$fieldNamePrefix2 = row.fieldNamePrefix) === null || _row$fieldNamePrefix2 === void 0
          ? void 0
          : _row$fieldNamePrefix2.slice(0, row.fieldNamePrefix.length - 2);
      if (
        parentNamePrefix === null || parentNamePrefix === void 0 ? void 0 : parentNamePrefix.length
      ) {
        (0, _set.default)(
          newValue,
          [].concat((0, _toConsumableArray2.default)(parentNamePrefix), [fieldName]),
          fieldValue,
        );
        form.setFieldsValue((0, _defineProperty2.default)({}, name, newValue));
      }
    },
    [name, form],
  );
  /** 批量递归设置子节点的某个字段 */
  var setChildrenValue = (0, _react.useCallback)(
    function (row, fieldName, fieldValue) {
      var value = form.getFieldValue(name) || [];
      var newValue = (0, _toConsumableArray2.default)(value);
      if (row.children) {
        var travser = function travser(list) {
          return list.map(function (item) {
            var _objectSpread2;
            return (0,
            _objectSpread3.default)((0, _objectSpread3.default)({}, item), {}, ((_objectSpread2 = {}), (0, _defineProperty2.default)(_objectSpread2, fieldName, fieldValue), (0, _defineProperty2.default)(_objectSpread2, 'children', item.children ? travser(item.children) : undefined), _objectSpread2));
          });
        };
        var children = travser(row.children);
        (0, _set.default)(newValue, row.childFieldNamePrefix, children);
        form.setFieldsValue((0, _defineProperty2.default)({}, name, newValue));
      }
    },
    [name, form],
  );
  /** 可编辑表格整体取值 */
  var getValue = (0, _react.useCallback)(
    function () {
      return form.getFieldValue(name);
    },
    [name, form],
  );
  /** 可编辑表格整体赋值 */
  var setValue = (0, _react.useCallback)(
    function (newValue) {
      return form.setFieldsValue((0, _defineProperty2.default)({}, name, newValue));
    },
    [name, form],
  );
  return {
    addItem: addItem,
    removeItem: removeItem,
    moveItem: moveItem,
    setParentValue: setParentValue,
    setChildrenValue: setChildrenValue,
    getValue: getValue,
    setValue: setValue,
  };
}
